package com.flowCloud.config.beetlsql;

import cn.hutool.core.util.StrUtil;
import com.flowCloud.config.interceptor.SecurityInterceptor;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.sql.core.*;
import org.beetl.sql.core.db.H2Style;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.core.loader.MarkdownClasspathLoader;
import org.beetl.sql.core.loader.SQLLoader;
import org.beetl.sql.ext.DebugInterceptor;
import org.beetl.sql.ext.spring.SpringConnectionSource;
import org.beetl.sql.ext.spring.SqlManagerFactoryBean;
import org.beetl.sql.starter.SQLManagerCustomize;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;

/**
 * @Author: Pompey_hpy
 * @Date: 2021/01/01
 * @Description:  BeetlSqlConfig
 */
@Configuration
@Slf4j
public class BeetlSqlConfig {


    @Bean
    public SQLManagerCustomize beetlSqlCustomize() {
        return new SQLManagerCustomize() {
            @Override
            public void customize(String dataSource,SQLManager sqlManager) {
                //可以在这里添加各种扩展，指定主从等
                sqlManager.setInters(new Interceptor[]{new BeetlSqlDebugInterceptor()});
            }

        };
    }


    @Bean(name = "ds1")
    public DataSource datasource(Environment env) {
        HikariDataSource ds = new HikariDataSource();
        ds.setJdbcUrl(env.getProperty("spring.datasource.url"));
        ds.setUsername(env.getProperty("spring.datasource.username"));
        ds.setPassword(env.getProperty("spring.datasource.password"));
        ds.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
        String connTimeOut = env.getProperty("spring.datasource.master.connection-timeout");
        String idleTimeout = env.getProperty("spring.datasource.master.idle-timeout");
        String maxLifetime = env.getProperty("spring.datasource.master.max-lifetime");
        String maxPoolSize = env.getProperty("spring.datasource.master.maximum-pool-size");
        String minIdle = env.getProperty("spring.datasource.master.minimum-idle");
        if (!StrUtil.isBlank(connTimeOut)){
            ds.setConnectionTimeout(Long.parseLong(connTimeOut));
        }
        if (!StrUtil.isBlank(idleTimeout)){
            ds.setIdleTimeout(Long.parseLong(idleTimeout));
        }
        if (!StrUtil.isBlank(maxLifetime)){
            ds.setMaxLifetime(Long.parseLong(maxLifetime));
        }
        if (!StrUtil.isBlank(maxPoolSize)){
            ds.setMaximumPoolSize(Integer.parseInt(maxPoolSize));
        }
        if(!StrUtil.isBlank(minIdle)){
            ds.setMinimumIdle(Integer.parseInt(minIdle));
        }
        log.info("dataSource is ready!");
        return ds;
    }



}
